package com.bawei.property.view

import android.util.Log
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.property.R
import com.bawei.property.adapter.ParkListAdapter
import com.bawei.property.databinding.FragmentParkApplyBinding
import com.bawei.property.entity.ParkValue
import com.bawei.property.intent.ParkViewIntent
import com.bawei.property.state.ParkUiState
import com.bawei.property.vm.ParkViewModel
import com.blankj.utilcode.util.GsonUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ParkApplyFragment : BaseMviFragment<FragmentParkApplyBinding>() {

    private val mViewModel: ParkViewModel by activityViewModels()
    private var mParkList = mutableListOf<ParkValue>()
    private lateinit var mParkListAdapter: ParkListAdapter


    companion object {
        @JvmStatic
        fun newInstance() = ParkApplyFragment()
    }

    override fun getLayoutId(): Int = R.layout.fragment_park_apply

    override fun initViews() {
        //设置布局管理器，决定布局样式
        mDataBinding.rvApplyList.layoutManager = GridLayoutManager(requireContext(), 6, GridLayoutManager.VERTICAL, false)
        //设置数据适配器
        mParkListAdapter = ParkListAdapter(mParkList)
        mParkListAdapter.setOnItemClickListener(object : ParkListAdapter.ItemParkOnClickListener{
            override fun onClick(v: View, position: Int) {
               var entity = mParkList[position]
               if (entity.parkUser > 0){
                   ARouter.getInstance().build(RoutePath.PATH_PARK_DETAIL_PAGE).withSerializable("parkEntity", entity).navigation()
               } else {
                   showToastMsg("当前车位可以被申请！")
               }
            }
        })

        mDataBinding.rvApplyList.adapter = mParkListAdapter

    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(ParkViewIntent.LoadParkList)
    }

    override suspend fun collectUiState() {
        mViewModel.uiState.collect{
            when(it){
                is ParkUiState.LoadParkListSuccess -> showParkList(it.data as List<ParkValue>)
                is ParkUiState.LoadParkListFail -> showToastMsg(it.msg)
                else -> {}
            }
        }
    }

    private fun showParkList(list: List<ParkValue>){
        if (list.isNotEmpty()){
            mParkList.clear()
            var newParkList = mutableListOf<ParkValue>()
            //计算一共有几行
            var rowNum = if (list.size % 5 > 0) list.size / 5 + 1 else list.size / 5
            var lastIndex = 0
            for (index in 1 .. rowNum){
                newParkList.add(ParkValue().apply {
                    parkUser = -1
                    parkName = "第${index}排"
                })
                lastIndex = if (index * 5 > list.size) list.size else index * 5
                newParkList.addAll(list.subList((index - 1) * 5, lastIndex))
            }
            mParkList.addAll(newParkList)
            mParkListAdapter.notifyDataSetChanged()

        } else {
            showToastMsg("当前没有车位信息")
        }
    }


}